<template>
  <el-card>
    <template #header>商品分类</template>
    <div>
      <el-button type="primary" size="mini" @click="dialogVisible = true"
        >添加分类</el-button
      >
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="40%">
        <div>
          <el-form
            :model="cartFrom"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="活动名称" prop="cateName">
              <el-input v-model="cartFrom.cateName"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch
                v-model="state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="alterfenly()">确 定</el-button>
        </span>
      </el-dialog>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="cateName" label="分类名称">
          <template v-slot="{ row }">
            <el-input v-model="row.cateName" v-if="!row.flag" size="mini">
            </el-input>
            <span v-else>{{ row.cateName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template v-slot="{ row }">
            <el-switch
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="row.flag"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'primary' : 'success'"
              @click="fn(row)"
              >{{ row.flag ? '编辑' : '完成' }}</el-button
            >
            <el-button type="danger" @click="delday(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
//
import {
  classification,
  delification,
  Goodslistadd,
  alterification
} from '@/api/Goods.js'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogVisible: false,
      state: true,
      // 数据对象
      cartFrom: {
        cateName: ''
      },
      // 规则对象
      rules: {
        cateName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fenye()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fenye()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fenye()
    },
    // 获取分类数据
    async fenye() {
      const { data, total } = await classification({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
      this.tableData.forEach(item => this.$set(item, 'flag', true))
    },
    // 删除分类
    delday(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          const data = await delification({
            id
          })
          location.reload()
          console.log(data)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加分类
    async alterfenly() {
      const { code, msg } = await Goodslistadd({
        cateName: this.cartFrom.cateName,
        state: this.state
      })
      if (code === 0) {
        location.reload()
        this.$message.success(msg)
        this.dialogVisible = false
      } else {
        this.$message.error(msg)
      }
    },
    async fn(row) {
      const { id, cateName, state } = row
      if (row.flag === false) {
        const { code, msg } = await alterification({
          id,
          cateName,
          state: Boolean(state)
        })
        if (code === 0) {
          this.$message.success(msg)
        } else {
          this.$message.error(msg)
        }
      }
      row.flag = !row.flag
    }
  }
}
</script>

<style lang="less" scoped></style>
